$(function () {
    //登录提交
    function submitData() {
        let mName = $('#mName').val();
        let mPassword = $('#mPassword').val();
        let json = {
            mName: mName,
            mPassword: mPassword,
            method: 'login'
        };
        $.post("/login", json, function (response) {
            let jsonObj = JSON.parse(response);
            if(jsonObj===0){
                $(".msgErr").show();
                setTimeout(() => {
                    $(".msgErr").hide();
                }, 2000);
                return false;
            }
            //根据返回的状态实现不同的业务
                localStorage.setItem('sId', jsonObj.sId);
                localStorage.setItem('state', jsonObj.state);
                window.location.href = './index.html';
        });
    }

    //根据校验结果是否发请求
    $("#submit").on("click", function () {
        let flag = $("#loginForm").data("bootstrapValidator").isValid();
        if (flag) submitData()
    })
//    表单校验
    $("#loginForm").bootstrapValidator({
        fields: {
            mName: {
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    }
                }
            },
            mPassword: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    }
                }
            }
        }
    })
//    注册跳转
    $("#register").on("click", function (e) {
        window.location.href = "./register.html";
    })
});